﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace landlord2
{
    /// <summary>
    /// ExtraWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ExtraWindow : Window
    {
        private DataSet m_dsPlayerInfo;
        private bool? m_rePosition;

        public ExtraWindow()
        {
            InitializeComponent();
            GameClient.Current.OnSendChatText += OnSendChatText;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Reposition();
        }

        public void Reposition()
        {
            if (m_rePosition == null) m_rePosition = SystemParameters.PrimaryScreenWidth > 1024;
            if (!m_rePosition.Value) return;
            Left = Owner.Left + Owner.Width;
            Top = Owner.Top;
            Height = Owner.Height;
        }

        private string GetPlayerName(int? seat)
        {
            if (seat == null) return "";
            return m_dsPlayerInfo.Tables[0].Rows[seat.Value]["Name"].ToString();
        }

        private void OnSendChatText(int? seat, string text)
        {
            Dispatcher.BeginInvoke((Action)delegate()
            {
                Paragraph paragraph;
                if (txtLog.Document.Blocks.Count == 0)
                {
                    paragraph = new Paragraph();
                    txtLog.Document.Blocks.Add(paragraph);
                }
                else paragraph = (Paragraph)txtLog.Document.Blocks.FirstBlock;
                Run run = new Run();
                if (seat == null)
                {
                    run.Text = text + "\r\n";
                    run.Foreground = (SolidColorBrush)App.Current.Resources["Log_Sys_Color"];
                }
                else
                {
                    run.Text = string.Format("{0}说：{1}\r\n", GetPlayerName(seat), text);
                    switch (seat.Value)
                    {
                        case 0: run.Foreground = (SolidColorBrush)App.Current.Resources["Log_Player1_Color"]; break;
                        case 1: run.Foreground = (SolidColorBrush)App.Current.Resources["Log_Player2_Color"]; break;
                        case 2: run.Foreground = (SolidColorBrush)App.Current.Resources["Log_Player3_Color"]; break;
                        case 3: run.Foreground = (SolidColorBrush)App.Current.Resources["Log_Player4_Color"]; break;
                    };
                }
                paragraph.Inlines.Add(run);
                txtLog.ScrollToEnd();
            });
        }

        public void ShowPlayerInfo(DataSet ds)
        {
            m_dsPlayerInfo = ds.Copy();
            lvwPlayers.ItemsSource = m_dsPlayerInfo.Tables[0].DefaultView;
        }

        private void txtChat_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                e.Handled = true;
                GameClient.Current.Client.SendChatText(txtChat.Text);
                txtChat.Text = "";
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
        }
    }
}
